/**
 * Copyright (c)  HeiFeiDeFengZheng.Co.Ltd. All rights reserved.
 **/

package com.dexing.hikaricp.utils;

import com.dexing.hikaricp.common.DataSourceType;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * 功能描述：
 *
 * @author huifeidefengzheng
 * @since 2024-02-17
 *
 **/
public class HikariCPDemo {
    public static void main(String[] args) {
        HikariConfig hikariConfig = new HikariConfig();
        hikariConfig.setDriverClassName(DataSourceType.MYSQL.getDataSourceClassName());
        hikariConfig.setAutoCommit(true);
        hikariConfig.setConnectionTestQuery("select 1");
        hikariConfig.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/test?serverTimezone=GMT%2B8&characterEncoding=utf-8");
        hikariConfig.setUsername("root");
        hikariConfig.setPassword("");

        HikariDataSource hikariDataSource = new HikariDataSource(hikariConfig);
        try {
            Connection connection = hikariDataSource.getConnection();
            Statement statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery("select * from user");
            // 5 处理结果集
            while (resultSet.next()) {
                String cid = resultSet.getString("id");
                String cname = resultSet.getString("user_name");
                System.out.println(cid + "-------------" + cname);
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

    }
}
